package com.usian.service.impl;

import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.redis.RedisClient;
import com.usian.service.SSOService;
import com.usian.utils.MD5Utils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
@MapperScan(value = "com.usian.mapper")
public class SSOServiceimpl implements SSOService {

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    RedisClient redisClient;

    @Value("${USER_INFO_KEY}")
    private String USER_INFO_KEY;

    /**
     * 注册验证接口
     *
     * @param checkValue
     * @param checkFlag
     * @return
     */
    @Override
    public boolean checkUserInfo(String checkValue, Integer checkFlag) {
        //checkFlag==1:校验用户名是否存在
        //checkFlag==2:校验用户手机号是否存在
        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        if (checkFlag == 1) {
            criteria.andUsernameEqualTo(checkValue);
        } else if (checkFlag == 2) {
            criteria.andPhoneEqualTo(checkValue);
        } else {
            return false;
        }
        List<TbUser> tbUsers = tbUserMapper.selectByExample(tbUserExample);
        return tbUsers.size() == 0;
    }

    /**
     * 用户注册接口
     *
     * @param tbUser
     * @return
     */
    @Override
    public Map userRegister(TbUser tbUser) {
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        String digest = MD5Utils.digest(tbUser.getPassword());
        tbUser.setPassword(digest);
        tbUserMapper.insertSelective(tbUser);
        return resultKey(tbUser);
    }

    /**
     * 用户登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Map userLogin(String username, String password) {
        //MD5生成对应密码
        String pwd = MD5Utils.digest(password);

        //查询数据库用户信息
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(pwd);
        TbUser user = tbUserMapper.selectByExample(example).get(0);
        //如果查不到直接返回null
        if (user == null) {
            return null;
        }
        return resultKey(user);
    }

    //由用户登录调用的方法
    public Map resultKey(TbUser user){
        //生成token 根据token 确定用户的活跃度
        String token = UUID.randomUUID().toString();
        //返回数据map类型
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("userid", user.getId());
        map.put("username", user.getUsername());
        //根据KEY+token 缓存用户信息
        redisClient.set(USER_INFO_KEY + ":" + token, user);
        //设置token的过期时间 用户没有活跃度 10分钟 需重新登录
        redisClient.expire(USER_INFO_KEY + ":" + token, 600L);
        return map;
    }

    /**
     * 用户退出接口
     *
     * @param token
     * @return
     */
    @Override
    public boolean logOut(String token) {
        return redisClient.del(USER_INFO_KEY + ":" + token);
    }

    /**
     * 更新用户过期时间
     *
     * @param token
     * @return
     */
    @Override
    public boolean getUserByToken(String token) {
        //先从redis获取用户信息是否存在
        TbUser user = (TbUser) redisClient.get(USER_INFO_KEY + ":" + token);
        //不存在返回null
        if (user == null) {
            return false;
        }
        //存在更新过期时间
        redisClient.set(USER_INFO_KEY + ":" + token, user);
        redisClient.expire(USER_INFO_KEY + ":" + token, 600L);
        return true;
    }
}
